package com.ukao.cashregister.ui.sendFactory;


import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.gprinterio.GpDevice;
import com.lcodecore.tkrefreshlayout.RefreshListenerAdapter;
import com.lcodecore.tkrefreshlayout.TwinklingRefreshLayout;
import com.ukao.cashregister.R;
import com.ukao.cashregister.adapter.ActoryBatchAdapter;
import com.ukao.cashregister.base.IAdapter;
import com.ukao.cashregister.base.IPage;
import com.ukao.cashregister.base.MvpFragment;
import com.ukao.cashregister.base.PageWrapper;
import com.ukao.cashregister.bean.BatchlTabBean;
import com.ukao.cashregister.listener.MyOnItemClickListener;
import com.ukao.cashregister.listener.OnItemChildClickListener;
import com.ukao.cashregister.pesenter.FactoryBatchPesenter;
import com.ukao.cashregister.printer.PrintMiuiHelper;
import com.ukao.cashregister.printer.PrinterReceipt;
import com.ukao.cashregister.ui.MainActivity;
import com.ukao.cashregister.utils.CheckUsbDeviceUtils;
import com.ukao.cashregister.utils.CheckUtils;
import com.ukao.cashregister.utils.T;
import com.ukao.cashregister.view.FactoryBatchView;
import com.ukao.cashregister.widget.TwinklingLayout;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

import static com.ukao.cashregister.consts.GlobalConsts.SEND_FACTORY_PAGE_SIZE;

/**

 */
public class FactoryBatchFragment extends MvpFragment<FactoryBatchPesenter> implements FactoryBatchView{


    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";

    Unbinder unbinder;
    @BindView(R.id.recycler_view)
    RecyclerView mRecyclerView;
    @BindView(R.id.twinkling_layout)
    TwinklingLayout twinklingLayout;
    public BatchlTabBean.ListBean mItem;
    private int pos;
    private String mParam1;
    private String mParam2;
    private ActoryBatchAdapter mActoryBatchAdapter;
    private List<BatchlTabBean.ListBean> mActoryBatchData = new ArrayList<>();
    private List<BatchlTabBean.ListBean>  mSaveBatchDataList=new ArrayList<>(); //保存批次数据集合
    private PageWrapper<BatchlTabBean.ListBean> mPageWrapper;
    private String  bagCode;
   private boolean isSearchBagNumber;  //是否搜索袋号


    public static FactoryBatchFragment newInstance(String param1, String param2) {
        FactoryBatchFragment fragment = new FactoryBatchFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }




    @Override
    public void onSupportVisible() {
        super.onSupportVisible();

        twinklingLayout.startRefresh();
    }


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1, "");
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
    }

    @Override
    protected FactoryBatchPesenter createPresenter() {
        return new FactoryBatchPesenter(this);
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.fragment_factory_tab, container, false);
        unbinder = ButterKnife.bind(this, view);
        initView();
        initListener();
        return view;
    }

    @Override
    protected void initView() {
        mActoryBatchData.clear();
        mActoryBatchAdapter = new ActoryBatchAdapter(activity, mActoryBatchData);
        mRecyclerView.setLayoutManager(new GridLayoutManager(activity, 2));

        mRecyclerView.setAdapter(mActoryBatchAdapter);
        IPage page = new IPage() {

            @Override
            public void load(int geIndex, int pageSize) {
                mvpPresenter.orderProductBatchIn(_mActivity, mParam1,geIndex,pageSize);
            }
        };
        mPageWrapper= new PageWrapper<>(mIAdapter, page);
        page.setPageSize(SEND_FACTORY_PAGE_SIZE);
    }

    /**
     * 请求搜索袋号
     */
    public  void  requestSearchBagNumber(String bagCode){
        this.bagCode=bagCode;
        isSearchBagNumber=true;
        twinklingLayout.startRefresh();
    }

    IAdapter mIAdapter = new IAdapter() {
        @Override
        public void setDataSource(List data, boolean isNextLoad) {
            mActoryBatchAdapter.setDatas(data);
            twinklingLayout.setEnableLoadmore(isNextLoad);
        }

        @Override
        public void loadMoreFinish(List data, boolean isNextLoad) {
            mActoryBatchAdapter.addDatas(data);
            twinklingLayout.setEnableLoadmore(isNextLoad);

        }
    };
    @Override
    protected void initListener() {
        super.initListener();
        mActoryBatchAdapter.setOnItemClickListener(mOnItemClickListener);
        mActoryBatchAdapter.setOnItemChildClickListener(mOnItemChildClickListener);
        twinklingLayout.setOnRefreshListener(mRefreshListenerAdapter);
    }

    private RefreshListenerAdapter mRefreshListenerAdapter=new RefreshListenerAdapter() {
        @Override
        public void onRefresh(TwinklingRefreshLayout refreshLayout) {
            super.onRefresh(refreshLayout);
                    if(isSearchBagNumber){
                        isSearchBagNumber=false;
                        mvpPresenter.requestBagNumber(_mActivity,mParam1,bagCode);
                        twinklingLayout.setEnableLoadmore(false);
                    }else {
                        mPageWrapper.loadPage(true);
                    }
        }

        @Override
        public void onLoadMore(TwinklingRefreshLayout refreshLayout) {
            super.onLoadMore(refreshLayout);
            mPageWrapper.loadPage(false);
        }



    };

    private OnItemChildClickListener mOnItemChildClickListener = new OnItemChildClickListener() {


        @Override
        public void onChildItemClick(int id, int pos) {
            mItem= mActoryBatchAdapter.getItem(pos);
            if (mItem==null)return;
            switch (id) {
                case R.id.isFinish:/*完成*/

                    mvpPresenter.orderProductComplete(_mActivity, mActoryBatchAdapter.getDatas().get(pos).getId() + "");
                    break;

                case R.id.delete_btn:/*删除*/
                    FactoryBatchFragment.this.pos = pos;
                    mvpPresenter.batchIndelete(_mActivity, mActoryBatchAdapter.getDatas().get(pos).getId() + "");
                    break;
                case R.id.print_btn:/*打印批号*/
                    if (CheckUtils.isMIUIDevices()){
                        PrintMiuiHelper.batchPrinting(MainActivity.getPrintService(),0,mItem);
                    }else if (CheckUsbDeviceUtils.isReceipt()){
                        GpDevice mDevice=new GpDevice();
                        PrinterReceipt.getInstance().printGpBatch(mItem,0,mDevice);

                    }
                    break;
            }


        }


    };

    private MyOnItemClickListener mOnItemClickListener = new MyOnItemClickListener() {
        @Override
        public void onItemClick(ViewGroup viewGroup, View view, Object o, int i) {
            BatchlTabBean.ListBean item = (BatchlTabBean.ListBean) o;
            if (getParentFragment() instanceof FactoryTabFragment) {
                ((FactoryTabFragment) getParentFragment()).start(BatchDetailsFragment.newInstance(item.getId() + "", item.getIsFinish(),item.getProductCnt()));
            }


        }


    };
    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    @Override
    public void loadSuccess(List<BatchlTabBean.ListBean> mData, int total) {
        mPageWrapper.addDataSource(mData, total);
    }

    @Override
    public void loadFinishSuccess(String message) {
        mItem.setIsFinish(1);
        mActoryBatchAdapter.notifyDataSetChanged();
        T.show(message);
    }

    @Override
    public void loadFail(String message) {
        T.show(message);
    }

    @Override
    public void deleteSuccess(String message) {
        mActoryBatchData.remove(pos);
        mActoryBatchAdapter.notifyDataSetChanged();
        T.show(message);


    }

    @Override
    public void finishLoad() {
        twinklingLayout.finishLoadmore();
        twinklingLayout.finishRefreshing();
    }

    /**
     * 接收单个袋号数据成功
     */
    @Override
    public void receiveBagNumberData(List<BatchlTabBean.ListBean> mData) {
        mActoryBatchAdapter.setDatas(mData);
    }
}
